package com.team7.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.common.entity.HeadNav;
import com.team7.common.vo.CommonResult;
import com.team7.common.vo.TouVo;
import com.team7.home.dao.HeadNavDao;
import com.team7.home.service.HeadNavService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: team7-object-master
 * @description:
 * @author: 王金涛
 * @create: 2021-10-19 14:48
 **/

@Service
public class HeadNavServiceImpl extends ServiceImpl<HeadNavDao, HeadNav> implements HeadNavService {

    @Resource
    private HeadNavDao headNavDao;


    @Override
    public CommonResult getAllHeadNav(Integer page, Integer limit, TouVo touVo) {
        //获取分页插件
        Page<HeadNav> page1 = new Page(page, limit);
        QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (StringUtils.isNotEmpty(touVo.getHeadName())){
            wrapper.like("head_name",touVo.getHeadName());
        }
        if (StringUtils.isNotEmpty(touVo.getIsDiable())){
            wrapper.like("is_diable",touVo.getIsDiable());
        }
        Page<HeadNav> headNavPage = headNavDao.selectPage(page1, wrapper);
        if (headNavPage.getRecords().size()>0){
            return new CommonResult(2000,"查询成功",headNavPage);
        }else {
            return new CommonResult(5000,"查询失败",null);
        }
    }

    /**
     * 头部导航更新方法
     * @param touVo
     * @return
     */
    @Override
    public CommonResult addHeadNav(TouVo touVo) {
        QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
        if (touVo.getHeadId()!=null){
            String headName = touVo.getHeadName();
            HeadNav headNav = new HeadNav();
            headNav.setHeadId(touVo.getHeadId());
            headNav.setHeadName(touVo.getHeadName());
            headNav.setHref(touVo.getHref());
            int i = headNavDao.updateById(headNav);
            if (i>0){
                return new CommonResult(2000,"更新成功",null);
            }else {
                return  new CommonResult(5000,"更新失败",null);
            }
        }else {
            //增加新增方法
            //判断前台传输的数据在数据库是否存在
            String headName = touVo.getHeadName();
            wrapper.eq("head_name",headName);
            HeadNav headNav = headNavDao.selectOne(wrapper);
            if (headNav!=null){
                return new CommonResult(5000,"添加导航栏失败",null);
            }else {
                HeadNav headNav1 = new HeadNav();
                headNav1.setHeadName(touVo.getHeadName());
                headNav1.setHref(touVo.getHref());
                headNav1.setIsDiable("0");
                headNav1.setIsDelete("0");
                int insert = headNavDao.insert(headNav1);
                if (insert>0){
                    return new CommonResult(2000,"添加成功",null);
                }else {
                    return new CommonResult(5000,"添加失败", null);
                }

            }
        }

    }

    @Override
    public CommonResult changeStatus(Integer headId, String isDiable) {
        HeadNav headNav = new HeadNav();
        headNav.setHeadId(headId);
        headNav.setIsDiable(isDiable);
        int i = headNavDao.updateById(headNav);
        if (i>0){
            return new CommonResult(2000,"修改成功",null);
        }else {
            return new CommonResult(5000,"修改失败", null);
        }
    }

    /**
     * 删除方法
     * @param headId
     * @return
     */
    @Override
    public CommonResult deleteHeadNav(Integer headId) {
        HeadNav headNav = new HeadNav();
        headNav.setHeadId(headId);
        headNav.setIsDelete("1");
        int i = headNavDao.deleteById(headId);
        if (i>0){
            return new CommonResult(2000,"删除成功",null);
        }else {
            return new CommonResult(5000,"删除成功", null);
        }
    }

    /**
     * 查询头部数据 显示
     * @return
     */
    @Override
    public CommonResult getAllHead() {
        QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("is_diable",0);
        List<HeadNav> headNavs = headNavDao.selectList(wrapper);
        return new CommonResult(2000,"查询成功",headNavs);
    }
}
